<!--
  - datlib.php
  - v0.01
  - 1/28/2011
  - This is a php file containing database functions.
-->
<?php
	/*Searches for users in the database.*/
	function LookFor($username, $password, $database, $host, $login,
			$loginpassword){
		//Create connection.
		$con = mysql_connect($host, $login, $loginpassword) or
			die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Set-up and execute connection.
		$query = "SELECT * FROM login where username = '". $username."'";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result) ;
		
		//Close the connection.
		mysql_close($con);
		if($row['password'] == md5($password)) return $row['usertype'];
		else return FALSE;
		
	}
	  
	function GetUsername($username, $database, $host, $login,
			$loginpassword){
		//Create connection.
		$con = mysql_connect($host, $login, $loginpassword) or
			die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Set-up and execute connection.
		$query = "SELECT name FROM users where username = '". $username."'";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result) ;
		
		//Close the connection.
		mysql_close($con);
		if($row['name'] != "") return $row['name'];
		else return FALSE;
		
	}
	
	/*Inserts a new account in the database*/
	function InsertNewUser($username, $password, $usertype, $name, $title, 
			$position, $database, $host, $login, $loginpassword){ 
		//Check the database for redundant entries.
		$foundusers = LookFor($username, $password, $database, $host, $login,
			$loginpassword);
		
		//If an entry was found
		if(count($foundusers) > 0 && $foundusers["password"] != "")
			return FALSE;
		
		//Create connection.
		$con = mysql_connect($host, $login, $loginpassword) or
			die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$password  = md5($password);
		//Insert the entry into the database.
		$loginQuery = "INSERT INTO login VALUES ('$username','$password','$usertype')";
		$userQuery = "INSERT INTO users VALUES ('$username','$name','$title','$position')";
		$login = mysql_query($loginQuery);
		$user = mysql_query($userQuery);
	
		//Close the connection.
		mysql_close($con);
		if($login == TRUE && $user == TRUE) return TRUE;
		else return FALSE;
	}
?>





